//
//  LTFExclusiveView.m
//  NeteaseNews
//
//  Created by liutaifeng on 16/4/26.
//  Copyright © 2016年 liutaifeng. All rights reserved.
//

#import "LTFExclusiveView.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "LTFExclusiveModel.h"
#import "LTFFirstTableViewCell.h"
#import "LTFThirdTableViewCell.h"
#import "LTFSecondTableViewCell.h"
#define WIDTH self.frame.size.width
#define HEIGHT self.frame.size.height
@interface LTFExclusiveView ()
<
 UITableViewDataSource,
 UITableViewDelegate
>
@property (nonatomic, retain) UIImageView *headImageView;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *tableViewArray;
@property (nonatomic ,retain) LTFExclusiveModel *model;
@property (nonatomic, retain) UIView *headView;
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, retain) UIButton *button;
@end


@implementation LTFExclusiveView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}
- (void)createView {
    //tableView
    self.tableView = [[UITableView alloc] initWithFrame:self.frame style:UITableViewStylePlain];
    [self addSubview:_tableView];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tag = 1008;
    //头视图
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 3)];
    [self addSubview:_headView];
    [_headView release];
    //图片
    self.headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 3)];
    [_headView addSubview:_headImageView];
    [_headImageView release];
    _tableView.tableHeaderView = _headView;
    
    //文字
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(5, HEIGHT / 3 - 40, WIDTH - 30, 30)];
    [_headView addSubview:_label];
    _label.textColor = [UIColor whiteColor];
    [_label release];
    
    //按钮
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    _button.frame = CGRectMake(0, 0, WIDTH, HEIGHT / 3);
    [_headView addSubview:_button];
    _button.backgroundColor = [UIColor clearColor];
    [_button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)click:(UIButton *)button {
    if (_model.photosetID != nil) {
        NSString *str = [_model.photosetID substringFromIndex:4];
        NSString *string =  [str stringByReplacingOccurrencesOfString:@"|" withString:@"/"];
        [self.delegate sendExclusiveAddress:string];
    }
    else {
        [self.delegate sendExclusiveAddress:_model.postid];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tableViewArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LTFExclusiveModel *model = self.tableViewArray[indexPath.row];
    if (model.imgextra == nil) {
        if (model.imgType == 0) {
            static NSString *firstReuse = @"firstReuse";
            LTFFirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:firstReuse];
            if (!cell) {
                cell = [[[LTFFirstTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:firstReuse] autorelease];
            }
            //图片
            [cell.picImageView setImageWithURL:[NSURL URLWithString:model.imgsrc] placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
            //标题
            
            cell.titleLabel.text = model.title;
            //内容
            if ([self.string isEqualToString:@"T1419386532423"]) {
                
            }
            else {
            cell.contentLabel.text = model.digest;
            }
            //跟帖
            if (model.TAGS != nil) {
                cell.countLabel.text = model.TAGS;
                cell.countLabel.textColor = [UIColor blueColor];
                
                cell.countLabel.layer.borderWidth = 1;
                cell.countLabel.layer.borderColor = [UIColor blueColor].CGColor;
                cell.countLabel.font = [UIFont systemFontOfSize:13];
                cell.countLabel.textAlignment = 1;
            } else {
                cell.countLabel.textColor = [UIColor grayColor];
                cell.countLabel.layer.borderWidth = 0;
                cell.countLabel.layer.borderColor = [UIColor clearColor].CGColor;
                cell.countLabel.text = [NSString stringWithFormat:@"%d跟帖", [model.replyCount intValue]];
            }
            return cell;
        }
        else {
            static NSString *secondReuse = @"secondReuse";
            LTFSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:secondReuse];
            if (!cell) {
                cell = [[[LTFSecondTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:secondReuse] autorelease];
            }
            //图片
            [cell.picImageView setImageWithURL:[NSURL URLWithString:model.imgsrc] placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
            //标题
            cell.titleLabel.text = model.title;
            //内容
            cell.contentLabel.text = model.digest;
            //跟帖数
            if (model.TAGS != nil) {
                cell.countLabel.text = model.TAGS;
                cell.countLabel.textColor = [UIColor blueColor];
                
                cell.countLabel.layer.borderWidth = 1;
                cell.countLabel.layer.borderColor = [UIColor blueColor].CGColor;
                cell.countLabel.font = [UIFont systemFontOfSize:13];
                cell.countLabel.textAlignment = 1;
            } else {
                cell.countLabel.textColor = [UIColor grayColor];
                cell.countLabel.layer.borderWidth = 0;
                cell.countLabel.layer.borderColor = [UIColor clearColor].CGColor;
                cell.countLabel.text = [NSString stringWithFormat:@"%d跟帖", [model.replyCount intValue]];
            }
            
            return cell;
        }
    }
    else {
        static NSString *thirdReuse = @"thirdReuse";
        LTFThirdTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:thirdReuse];
        if (!cell) {
            cell = [[[LTFThirdTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:thirdReuse] autorelease];
        }
        cell.titleLabel.text = model.title;

            cell.countLabel.textColor = [UIColor grayColor];
        if (model.TAGS != nil) {
            cell.countLabel.text = model.TAGS;
            cell.countLabel.textColor = [UIColor blueColor];
            
            cell.countLabel.layer.borderWidth = 1;
            cell.countLabel.layer.borderColor = [UIColor blueColor].CGColor;
            cell.countLabel.font = [UIFont systemFontOfSize:13];
            cell.countLabel.textAlignment = 1;
        } else {
            cell.countLabel.textColor = [UIColor grayColor];
            cell.countLabel.layer.borderWidth = 0;
            cell.countLabel.layer.borderColor = [UIColor clearColor].CGColor;
            cell.countLabel.text = [NSString stringWithFormat:@"%d跟帖", [model.replyCount intValue]];
        }
        
        [cell.firstImageView setImageWithURL:[NSURL URLWithString:model.imgsrc] placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
        [cell.secondImageView setImageWithURL:[NSURL URLWithString:[model.imgextra firstObject][@"imgsrc"]] placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
        [cell.thirdImageView setImageWithURL:[NSURL URLWithString:[model.imgextra lastObject][@"imgsrc"]] placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
        return cell;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    LTFExclusiveModel *model = self.tableViewArray[indexPath.row];
    if (model.imgextra == nil) {
        if (model.imgType == 0) {
            return 130;
        } else {
            return 240;
        }
    } else {
        return 180;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LTFExclusiveModel *model = self.tableViewArray[indexPath.row];
    [self.delegate sendAddress:model.docid];
}

- (void)setString:(NSString *)string {
    if (_string != string) {
        [_string release];
        _string = [string retain];
        [self createData];
    }
}
- (void)createData {
    self.tableViewArray = [NSMutableArray array];
    NSString *str = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/list/%@/0-20.html", self.string];
    //请求
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:str parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dictionary = responseObject;
        NSArray *array = [dictionary objectForKey:self.string];
        self.tableViewArray = [LTFExclusiveModel BaseModelByArray:array];
        
        if (self.tableViewArray.count != 0) {
            self.model = self.tableViewArray[0];
            [self.headImageView setImageWithURL:[NSURL URLWithString:_model.imgsrc] placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
            self.label.text = _model.title;
            [self.tableViewArray removeObjectAtIndex:0];
            [self.tableView reloadData];
            self.tableView.contentOffset = CGPointMake(0, 0);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
        
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
